import numpy


class PCARequestBody:

    def __init__(self, xfile, yfile, ppmfile, thread_path):
        # 构建PCA模型的必须元素
        self.xfile = xfile
        self.yfile = yfile
        self.ppmfile = ppmfile
        self.thread_path = thread_path
        self._ncomps = 2
        self._scaling_type = 0
        self._dpi = 1000

        # score_plot中需要的元素
        self.plot_title = None
        # 需要减去一符合输入维数的习惯
        self._plot_component_x = 0
        # 需要减去一符合输入维数的习惯
        self._plot_component_y = 1
        self.score_plot_comps = [self._plot_component_x, self._plot_component_y]
        self._discrete = False
        # 需要减去一符合输入维数的习惯
        self._discrete_label = None
        self._label_outliers = False
        self._outliers_index = None

        # scree_plot中需要的元素
        self._scree_total_comps = 5

        # cv_plot中需要的元素
        self._cv_repeats = 5
        self._cv_total_comps = 7

        # outlier中需要的元素
        self._outlier_comps = None
        self._outlier_measure = "T2"
        self._outlier_alpha = 0.05

        # dmodex_plot中需要的元素
        self._dmodex_label_outliers = True
        self._dmodex_alpha = 0.05

    @property
    def ncomps(self):
        return self._ncomps

    @ncomps.setter
    def ncomps(self, n):
        self._ncomps = int(n)

    @property
    def scaling_type(self):
        return self._scaling_type

    @scaling_type.setter
    def scaling_type(self, n):
        self._scaling_type = float(n)

    @property
    def dpi(self):
        return self._dpi

    @dpi.setter
    def dpi(self, n):
        self._dpi = int(n)

    @property
    def plot_component_x(self):
        return self._plot_component_x

    @plot_component_x.setter
    def plot_component_x(self, n):
        self._plot_component_x = int(n) - 1
        self.score_plot_comps = [self._plot_component_x, self._plot_component_y]

    @property
    def plot_component_y(self):
        return self._plot_component_y

    @plot_component_y.setter
    def plot_component_y(self, n):
        self._plot_component_y = int(n) - 1
        self.score_plot_comps = [self._plot_component_x, self._plot_component_y]

    @property
    def discrete(self):
        return self._discrete

    @discrete.setter
    def discrete(self, value):
        self._discrete = bool(value)

    @property
    def discrete_label(self):
        return self._discrete_label

    @discrete_label.setter
    def discrete_label(self, value):
        if self._discrete is True:
            self._discrete_label = int(value) - 1

    @property
    def label_outliers(self):
        return self._label_outliers

    @label_outliers.setter
    def label_outliers(self, value):
        self._label_outliers = bool(value)

    @property
    def outliers_index(self):
        return self._outliers_index

    @outliers_index.setter
    def outliers_index(self, value):
        if value != "None":
            self._outliers_index = self.str2intList(value)

    @property
    def scree_total_comps(self):
        return self._scree_total_comps

    @scree_total_comps.setter
    def scree_total_comps(self, value):
        self._scree_total_comps = int(value)

    @property
    def cv_repeats(self):
        return self._cv_repeats

    @cv_repeats.setter
    def cv_repeats(self, value):
        self._cv_repeats = int(value)

    @property
    def cv_total_comps(self):
        return self._cv_total_comps

    @cv_total_comps.setter
    def cv_total_comps(self, value):
        self._cv_total_comps = int(value)

    @property
    def outlier_comps(self):
        return self._outlier_comps

    @outlier_comps.setter
    def outlier_comps(self, value):
        if value != "None":
            self._outlier_comps = int(value)

    @property
    def outlier_measure(self):
        return self._outlier_measure

    @outlier_measure.setter
    def outlier_measure(self, value):
        self._outlier_measure = value

    @property
    def outlier_alpha(self):
        return self._outlier_alpha

    @outlier_alpha.setter
    def outlier_alpha(self, value):
        self._outlier_alpha = float(value)

    @property
    def dmodex_label_outliers(self):
        return self._dmodex_label_outliers

    @dmodex_label_outliers.setter
    def dmodex_label_outliers(self, value):
        self._dmodex_label_outliers = bool(value)

    @property
    def dmodex_alpha(self):
        return self._dmodex_alpha

    @dmodex_alpha.setter
    def dmodex_alpha(self, value):
        self._dmodex_alpha = float(value)

    def set_data(self, data):
        for key, value in data.items():  # 遍历数据字典
            if hasattr(self, key):  # 如果存在同名属性
                setattr(self, key, value)  # 则添加属性到对象中

    def str2intList(self, stringWaitToTrans):
        temp_list = stringWaitToTrans.strip(',').split(',')
        final_list = []
        for i in temp_list:
            final_list.append(int(i))
        return numpy.array(final_list)
